#include "Target.h"
#include "macro.h"
#include "Bot.h"
#include "const.h"

Target::Target()
{
    //ctor
}

//extern std::ofstream debug;//2("debug2.txt");

void Target::doMoveAnt(const Ant& old, const Ant& newant)
{
    if (old != newant)
    {
        if ( m_food.find(old) != m_food.end())
        {
//            debug << __FUNCTION__ << m_food.size() << " "<< old.row << ":" << old.col << "->"<< newant.row << ":" << newant.col << std::endl;

            //add(newant, m_food[old]);
            m_food[newant] = m_food[old];
            food_targets.push_back(m_food[old]);
            m_food.erase(old);
        }
    }
    else
    {
//        debug << "Warning: HANG!!!!" << std::endl;
        //del(old);
    }
}


void Target::clear(const AntVector& ant, const FoodVector& food)
{
    // remove dead ants
    // TODO: see deadants array
    AntFoodMap::iterator del;
    FOR_EACH(d,ant)
    {
        if( m_food.end() != (del = m_food.find(*d)))
            m_food.erase(del);
    }

    food_targets.clear();
}


void Target::del(const Ant& ant)
{
    AntFoodMap::iterator f = m_food.find(ant);

    if ( f != m_food.end())
    {
//        debug << __FUNCTION__ << " Delete target " << f->first.row<< ":" << f->first.col << "<->" << f->second.row<< ":" << f->second.col << std::endl;
        m_food.erase(f);
    }

}

void Target::add(const Ant& ant, const Food& food)
{
//    debug << __FUNCTION__ << " ["<< ant.row << ":" << ant.col << "] task " << food.row << ":" << food.col << std::endl;
    m_food[ant] = food;
    food_targets.push_back(food);
}

bool Target::isAssigned(const Food& food) const
{
    return IN_ARRAY(food,food_targets);
}

bool Target::hasTask(const Ant& ant) const
{
    return m_food.find(ant) != m_food.end();
}
/*
// return new direction if ant has task, 4 (DIRECTION_UNKNOWN) otherwise
int Target::getDirection(const Ant& ant, const State* state) const
{
    AntFoodMap::const_iterator f = m_food.find(ant);

    if ( f == m_food.end())
        return DIRECTION_UNKNOWN;
    else
        return state->GetDirection(ant, f->second);
}
*/
